package com.linkvin.ec.hcs.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.linkvin.ec.hcs.dao.model.sysadmin.Permission;
import com.linkvin.ec.hcs.dao.model.sysadmin.Role;
import com.linkvin.ec.hcs.vo.RestVO;
import com.linkvin.ec.hcs.service.sysadmin.RoleService;
import com.linkvin.ec.hcs.vo.RolePermRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by zhaoguixin on 2017/12/7.
 */

@Controller
@RequestMapping(value = "/role", produces = MediaType.APPLICATION_JSON_VALUE)
public class RoleController {

    @Autowired
    private RoleService roleService;


    @PostMapping(value = "/queryPage",consumes = "application/json")
    @ResponseBody
    public RestVO<Page<Role>> queryRoleList(@RequestBody Page<Role> page){
        RestVO<Page<Role>> result = new RestVO<>(0,"success");
        try{
            result.setData(roleService.queryRolePage(page));
        }catch(Exception ex)
        {
            result.setCode(-1);
            result.setMessage("error");
        }
       return result;
    }

    //TODO 创建角色 zgx
    @PostMapping(value = "/create",consumes = "application/json")
    @ResponseBody
    public RestVO<Role> createRole(@RequestBody Role role){
        RestVO<Role> result = new RestVO<>(0,"success");
        try {
            result.setData(roleService.addRole(role));
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
            return result;
        }
       return result;
    }

    //TODO 创建角色 zgx
    @PostMapping(value = "/update",consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> updateRole(@RequestBody Role role){
        RestVO<Integer> result = new RestVO<>(0,"success");
        try {
            int i =roleService.updateRole(role);
            result.setData(i);
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
            return result;
        }
        return result;
    }

    //TODO 创建角色 zgx
    @PostMapping(value = "/delete",consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> deleteRole(@RequestBody List<Integer> ids){
        RestVO<Integer> result = new RestVO<>(0,"success");
        try {
            int i =roleService.delRole(ids);
            result.setData(i);
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
            return result;
        }
        return result;
    }

    //TODO 创建角色 zgx
    @GetMapping(value = "/permission/own")
    @ResponseBody
    public RestVO<List<Integer>> getOwnPermissionIds(@RequestParam("roleId") Integer roleId){
        RestVO<List<Integer>> result = new RestVO<>(0,"success");
        try {
            List<Permission> ownPermission = roleService.listOwnPermission(roleId);
            if(!Objects.equals(ownPermission,null)){
                List<Integer> permissionIds = new ArrayList<>();
                for (Permission permission: ownPermission) {
                    permissionIds.add(permission.getId());
                }
                result.setData(permissionIds);
            }
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    //TODO 创建角色 zgx
    @PostMapping(value = "/permission/update",consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> updateRolePermission(@RequestBody RolePermRequestVO reVO){
        RestVO<Integer> result = new RestVO<>(0,"success");
        try {
            int i = roleService.assignPermission(reVO.getRoleId(),reVO.getPermissionIds());
            result.setData(i);
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
            return result;
        }
        return result;
    }

    @GetMapping(value = "/all")
    @ResponseBody
    public RestVO<List<Role>> listAll(){
        RestVO<List<Role>> result = new RestVO<>(0,"success");
        try {
            result.setData(roleService.listAll());
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
            return result;
        }
        return result;
    }
}
